package org.gdgbologna.devfest.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

import org.gdgbologna.devfest.R;
import org.gdgbologna.resources.Session;
import org.gdgbologna.resources.Talk;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class TalkAdapter extends BaseAdapter {
	
	private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;
    private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

	class ViewHolder {
		TextView 	title;
		TextView 	speaker;
		TextView	time;
		LinearLayout	circle;
		
		ViewHolder(View row){
			title = (TextView) row.findViewById(R.id.talk_title);
			speaker = (TextView) row.findViewById(R.id.talk_speaker);
			time = (TextView) row.findViewById(R.id.talk_time);
			circle = (LinearLayout) row.findViewById(R.id.circle);
		}
	}
	
	class HeaderViewHolder{
		TextView	title;
		ImageView	image;
		TextView 	subtitle;
		
		HeaderViewHolder(View row){
			title = (TextView) row.findViewById(R.id.title_header);
			image = (ImageView) row.findViewById(R.id.image_header);
			subtitle = (TextView) row.findViewById(R.id.subtitle_header);
		}
	}

	private LayoutInflater mInflater;
	private List<Object> talks = new ArrayList<Object>();
	private Activity act;
	
	private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

	public TalkAdapter(Context context) {
		// Cache the LayoutInflate to avoid asking for a new one each time.
		if (context == null) Log.e("DEVFEST","contexts is null");
		mInflater = LayoutInflater.from(context);
		act =  (Activity) context;
	}
	
	public void addItem(final Talk t){
		talks.add(t);
	}

	public void addSeparatorItem(final Session s){
		talks.add(s);
		mSeparatorsSet.add(talks.size()-1);
	}
	
	public void clear(){
		if (talks != null) talks.clear();
		if (mSeparatorsSet != null) mSeparatorsSet.clear();
		this.notifyDataSetChanged();
	}
	
	@Override
	public int getItemViewType(int position){
		return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
	}
	
	@Override
    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }

	/**
	 * The number of items in the list
	 */
	public int getCount() {
		return talks.size();
	}

	/**
	 * Get an item
	 */
	public Object getItem(int position) {
		return talks.get(position);
	}

	/**
	 * Get the id of this mainType (defined in types.xml)
	 */
	public long getItemId(int position) {
		if (getItemViewType(position) == TYPE_SEPARATOR) return 2000+position;
		else return ((Talk)getItem(position)).getId();
	}

	/**
	 * Make a view to hold each row.
	 * 
	 * @see android.widget.ListAdapter#getView(int, android.view.View,
	 *      android.view.ViewGroup)
	 */
	@SuppressWarnings("deprecation")
	@TargetApi(16)
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		HeaderViewHolder header_holder = null;
		
		int type = getItemViewType(position);

		if (convertView == null) {
            switch (type) {
                case TYPE_ITEM:
                    convertView = mInflater.inflate(R.layout.row_list, null);
                    holder = new ViewHolder(convertView);
                    convertView.setTag(holder);
                    break;
                case TYPE_SEPARATOR:
                    convertView = mInflater.inflate(R.layout.header_list, null);
                    header_holder = new HeaderViewHolder(convertView);
                    convertView.setTag(header_holder);
                    break;
            }
            
        } else {
        	if (type == TYPE_ITEM) holder = (ViewHolder)convertView.getTag();
        	else header_holder = (HeaderViewHolder )convertView.getTag();
        }

		Object obj = talks.get(position);
		if (obj instanceof Talk){
			Talk talk = (Talk) obj;
			holder.title.setText(talk.getTitle());
			holder.time.setText(talk.getTime());
			if (talk.getSpeaker().equalsIgnoreCase("")) holder.speaker.setText(talk.getAula());
			else holder.speaker.setText(talk.getSpeaker() + " - "+talk.getAula());
			
			ShapeDrawable circle = new ShapeDrawable( new  OvalShape() );
			circle.getPaint().setColor(Color.BLACK);			
			
			if (talk.getColor().equalsIgnoreCase("green")) circle.getPaint().setColor(act.getResources().getColor(R.color.green));
			if (talk.getColor().equalsIgnoreCase("red")) circle.getPaint().setColor(act.getResources().getColor(R.color.red));
			if (talk.getColor().equalsIgnoreCase("blue")) circle.getPaint().setColor(act.getResources().getColor(R.color.blue));
			if (talk.getColor().equalsIgnoreCase("yellow")) circle.getPaint().setColor(act.getResources().getColor(R.color.yellow));
			
			if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
				holder.circle.setBackgroundDrawable(circle);
			} else {
				holder.circle.setBackground(circle);
			}
		}else{
			Session s = (Session) obj;
			header_holder.title.setText(s.getTitle());
			header_holder.subtitle.setText(s.getDate());
		}
				
		return convertView;
	}

}
